3D bars

An example of using Matplotlib to create 3D plots.

Taken from mplot3d example code: bars3d_demo.py


In [ ]:
# options: inline (non-interactive), notebook & widget
%matplotlib widget

# The basic needed imports
import matplotlib.pyplot as plt
#from mpl_toolkits.mplot3d import Axes3D
import numpy as np

#%config InlineBackend.print_figure_kwargs={'bbox_inches':None}
plt.rcParams['figure.figsize'] = [10, 7]
plt.rcParams['savefig.dpi'] = 80
plt.rcParams['figure.dpi']  = 80

In [ ]:
from mpl_toolkits.mplot3d import Axes3D

# The figure to plot
fig1 = plt.figure(figsize=(10,7), dpi=80)
ax1 = fig1.add_subplot(111, projection='3d');
#fig.set_size_inches(15,7)
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
ax1.set_zlabel('Z');
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
    xs = np.arange(20)
    ys = np.random.rand(20)

    # You can provide either a single color or an array. To demonstrate this,
    # the first bar of each set will be colored cyan.
    cs = [c] * len(xs)
    cs[0] = 'c'
    ax1.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)

In [ ]:
from mpl_toolkits.mplot3d import Axes3D

fig2 = plt.figure(figsize=(10,7), dpi=80)
ax2 = plt.axes(projection='3d')

# Data for a three-dimensional line
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax2.plot3D(xline, yline, zline, 'gray')

# Data for three-dimensional scattered points
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax2.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens');

In [ ]: